package com.study.java.jinjie.file;



import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class MyFile {
    public static void main(String[] args){
       //create();
       //dir1();

       /*String s = "zfc";
       String ss = ss();

       byte[] bytes = new byte[10];
       byte[] bytes2 = {1,2};
       byte[] bytes3 = bytes();*/

        //writeFile();
//        readFile();


        String dir = "d:\\testDir";
        //创建一个文件夹Student
        //Student.txt写入内容
        //Student.csv写入内容


        Set<String> dirs = new HashSet<>();
        dirs.add("zhangsan");
        dirs.add("zhangsan2");
        dirs.add("zhangsan3");
        dirs.add("zhangsan4");
        dirs.add("zhangsan5");

//        String dir = "d:\\testDir";
        testDir(dir, dirs);
    }


    public static void testDir(String dir, Set<String> dirs){
       Iterator<String> iterator = dirs.iterator();
       while (iterator.hasNext()) {
           String mu = String.valueOf(iterator.next());
           File dirl = new File(dir, mu);
           System.out.println(mu);
           dirl.mkdir();
       }
    }


    public static String ss(){
        return "zfc";
    }

    public static byte[] bytes(){
        byte[] bytes = new byte[10];
        return bytes;
    }




    // 目录操作
    public static void dir(){
        File file1 = new File("C:\\Users\\azez1\\Desktop\\项目文件");

        try {
            // 获取目录文件(夹)列表
            //file1.list();    // 返回String[],
            //file1.listFiles();   //返回File[]
            file1.listFiles(); // File[]
            // 目录, 不为空, 长度不为0
            File[] h = file1.listFiles();
            if (h!=null) {
                for (int i = 0; i < h.length; i++) {
                    System.out.println("-"+h[i].getName());
                    File[] d = new File(h[i].getName()).listFiles();
                    if (d!=null) {
                        for (int j = 0; j < d.length; j++) {
                            System.out.println("|----"+d[j].getName());
                            File f = d[j];
                        }
                    }
                }
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    // 使用list 目录操作
public static void dir1() {
    File file2 = new File("C:\\Users\\azez1\\Desktop\\项目文件");
    String[] k = file2.list();
    try {
        if (k != null) {
            for (int i = 0; i < k.length; i++) {
                System.out.println("--"+k[i]);
//                String[] l = new File(file2.getAbsolutePath() + "\\" + k[i]).list();
                String[] l = new File(file2.getPath() + File.separator + k[i]).list();
                if (l != null) {
                    for (int j = 0; j < l.length; j++) {
                        System.out.println("     |____"+l[j].toString());
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

    // 创建文件
    public static void create(){
        File file1 = new File("C:\\Users\\azez1\\Desktop\\项目文件\\test.txt");
        File file2 = new File("C:\\Users\\azez1\\Desktop\\项目文件","text3.txt");

        // 创建, 删除, 判断

        //  创建 .createNewFile()
        // 判断 .exists()
        // 删除 .delete()
        //  读取
        // 写入

        try {
            // 判断文件是否存在
            if (file1.exists()){
                file1.delete();
                file1.createNewFile();
                System.out.println("文件已存在");
            } else {
                file1.createNewFile();
                System.out.println("创建成功!");
            }
            


        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public static void writeFile(){
        // 文件 File 创建,重命名,删除
        // 内容:
        //    写入: FileOutputStream
        //    读取: FileInputStream

        System.out.println();
        String content = "这是写入文件的内容。489498498849849";
        FileOutputStream fos = null;
        try {
            byte[] bytes = content.getBytes();  // 将字符串转为字节数组
            fos = new FileOutputStream("test.txt");  // 获取文件对象
            fos.write(bytes); // 写入字节数组
            System.out.println("文件写入完成！");
        } catch (IOException e) {
            System.out.println("文件写入失败：" + e.getMessage());
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }

    }

    public static void readFile(){
        FileInputStream fis = null;
        try {
            byte[] buffer = new byte[1024];

            fis = new FileInputStream("test.txt");

            int length;

            while ((length = fis.read(buffer)) != -1) {
                System.out.println(new String(buffer, 0, length)); // 将读取到的字节数组转为字符串输出
            }
            System.out.println("文件读取完成！");
        } catch (IOException e) {
            System.out.println("文件读取失败：" + e.getMessage());
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    }
}
